package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.UserInfo;
import com.ebring.shareEducation.dao.UserInfoMapper;
import com.ebring.shareEducation.service.UserInfoService;
import com.ebring.shareEducation.utils.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Transactional
public class UserInfoServiceImpl implements UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 注册登录信息
     *
     * @param
     * @return
     */
    @Override
    public RspMsg register(String yzm, String codeId,String email,String password,String  nation,String peincode) {
        RspMsg rspMsg = new RspMsg();
        String pwdMes = Md5Utils.GetMD5Code("%$#58412365" + yzm);
        if (!codeId.equals(pwdMes)) {
            rspMsg.setError();
            rspMsg.setMsg("验证码错误");
            return rspMsg;
        }
        String id="";
        List<UserInfo> userInfo = userInfoMapper.findUser(id,email);
        if (userInfo != null&&userInfo.size()>0) {
            rspMsg.setError();
            rspMsg.setMsg("该邮箱已被注册，请更换重试");
            return rspMsg;
        }
        UserInfo userInfo1 = new UserInfo();
        String userId = UuidUtils.getNumberId(6);
        userInfo1.setId(userId);
        //用戶名
        userInfo1.setRealName(UuidUtils.getRandomZm() + UuidUtils.getRandom());
        //新用戶密码为11111
        userInfo1.setPassword(password);
        // 将新密码生成盐值并且加密
        userInfo1.setRemark1(UUID.randomUUID().toString().replaceAll("-", ""));
        userInfo1.setPassword(Md5Utils.encryptPassword(userInfo1.getPassword(), userInfo1.getRemark1()));
        userInfo1.setNationality(nation);
        userInfo1.setEmail(email);
        userInfo1.setRegDate(DateUtil.getStringDate());
        userInfo1.setPeinCode(peincode);
        userInfo1.setReinCode(ShareCodeUtil.toSerialCode(Long.parseLong(userId)));
        int i = userInfoMapper.insertSelective(userInfo1);
        if (i > 0) {
            rspMsg.setOK();
            rspMsg.setMsg("用户注册成功");
            rspMsg.setResult(userId);
            return rspMsg;
        }
        return rspMsg;
    }


    // 更新用户
    @Override
    public RspMsg<UserInfo> updateUser(UserInfo userInfo) {
        RspMsg rspMsg = new RspMsg();
        UserInfo  userInfo1 =userInfoMapper.selectByPrimaryKey(userInfo.getId());
        userInfo1.setLimit(userInfo.getLimit());
        userInfo1.setSpecial(userInfo.getSpecial());
        userInfo1.setNoFee(userInfo.getNoFee());
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo1);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("更新信息失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("更新信息成功");

        return rspMsg;
    }

    @Override
    public RspMsg<UserInfo> insertCert(String userid,String realname,String certnumber,String image1,String image2,String image3) {
        RspMsg rspMsg = new RspMsg();
        UserInfo  userInfo1 =userInfoMapper.selectByPrimaryKey(userid);
        userInfo1.setRealName(realname);
        userInfo1.setCertNo(certnumber);
        userInfo1.setCertType("身份证");
        userInfo1.setCertDate(DateUtil.getStringDate());
        userInfo1.setImage1(image1);
        userInfo1.setImage2(image2);
        userInfo1.setImage3(image3);

        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo1);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("更新信息失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("更新信息成功");

        return rspMsg;
    }

    @Override
    public RspMsg<UserInfo> updateCert(String userid) {
        RspMsg rspMsg = new RspMsg();
        UserInfo  userInfo1 =userInfoMapper.selectByPrimaryKey(userid);
        userInfo1.setState(1);
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo1);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("更新信息失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("更新信息成功");
        return rspMsg;
    }

    @Override
    public RspMsg<UserInfo> updateLocking(String userid, String locking) {
        RspMsg rspMsg = new RspMsg();
        UserInfo  userInfo1 =userInfoMapper.selectByPrimaryKey(userid);
        userInfo1.setLocking(locking);
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo1);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("更新信息失败");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setMsg("更新信息成功");
        return rspMsg;    }


    // 根据ID查找用户
    @Override
    public RspMsg<UserInfo> selectUserByUserId(String userId) {
        RspMsg<UserInfo> rspMsg = new RspMsg<>();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        if (StringUtils.isEmpty(userInfo)) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到数据");
            return rspMsg;
        }
        rspMsg.setOK();
        rspMsg.setResult(userInfo);
        return rspMsg;
    }

    // 批量删除用户
    @Override
    public RspMsg<UserInfo> DeleteUsers(String[] userId) {

        RspMsg rspMsg = new RspMsg();
        int sum = 0;
        for (String s : userId) {
            userInfoMapper.deleteByPrimaryKey(s);
            sum++;
        }

        if (sum > 0) {
            rspMsg.setOK();
            rspMsg.setResult(sum);
            return rspMsg;
        }

        rspMsg.setError();
        rspMsg.setMsg("删除失败");
        return rspMsg;
    }

    // 用户登陆
    @Override
    public     RspMsg<UserInfo> loginInfo(String email, String password, String codeId){
        RspMsg rspMsg = new RspMsg();
        String id="";
        Map<String, Object> map = new LinkedHashMap<>();
        // 将新密码生成盐值并且加密
        UserInfo userInfo=userInfoMapper.findUser(id,email).get(0);
        if (userInfo == null) {
            rspMsg.setError();
            rspMsg.setMsg("当前用户不存在");
            return rspMsg;
        }
        String srpassword=Md5Utils.encryptPassword(password, userInfo.getRemark1());
        String oldpassword=userInfo.getPassword();
        String token = JwtUtils.sign(userInfo, 30L * 24L * 3600L * 1000L);


        if (!srpassword.equals(oldpassword)) {
            rspMsg.setError();
            rspMsg.setMsg("密码输入有误，请重新输入");
            return rspMsg;
        }else{
            map.put("data", userInfo);
            map.put("token", token);
            rspMsg.setOK();
            rspMsg.setMsg("登录成功");
            rspMsg.setResult(map);
            return rspMsg;
        }



    }

    // 修改密码

    @Override
    public  RspMsg<UserInfo> changePasswordInfo(String userinfoId,String oldPassword, String newPassword,  String token){
    RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new HashMap<>();
        if (token == null || "".equals(token)) {
            rspMsg.setError();
            rspMsg.setMsg("用户未登陆,请先登录");
            return rspMsg;
        }
        UserInfo userInfo = JwtUtils.unsign(token, UserInfo.class);
        if (!Md5Utils.encryptPassword(oldPassword, userInfo.getRemark2()).equals(userInfo.getPassword())) {
            rspMsg.setError();
            rspMsg.setMsg("原密码错误,请重新输入");
            return rspMsg;
        } else {
            userInfo.setPayPassword(newPassword);
            // 将新密码生成盐值并且加密
            userInfo.setRemark2(UUID.randomUUID().toString().replaceAll("-", ""));
            userInfo.setPassword(Md5Utils.encryptPassword(userInfo.getPassword(), userInfo.getRemark2()));
            int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
            if (i == 0) {
                rspMsg.setError();
                rspMsg.setMsg("支付密码修改失败");
                return rspMsg;
            }
            rspMsg.setOK();
            rspMsg.setMsg("支付密码修改成功");
            return rspMsg;
        }

    }



    // 修改密码

    @Override
    public    RspMsg<UserInfo> changepayPasswordInfo(String userinfoId,String oldPassword, String newPassword,  String token){
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new HashMap<>();
        if (token == null || "".equals(token)) {
            rspMsg.setError();
            rspMsg.setMsg("用户未登陆,请先登录");
            return rspMsg;
        }
        UserInfo userInfo = JwtUtils.unsign(token, UserInfo.class);
        if (!Md5Utils.encryptPassword(oldPassword, userInfo.getRemark1()).equals(userInfo.getPassword())) {
            rspMsg.setError();
            rspMsg.setMsg("原密码错误,请重新输入");
            return rspMsg;
        } else {
            userInfo.setPassword(newPassword);
            // 将新密码生成盐值并且加密
            userInfo.setRemark1(UUID.randomUUID().toString().replaceAll("-", ""));
            userInfo.setPassword(Md5Utils.encryptPassword(userInfo.getPassword(), userInfo.getRemark1()));
            int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
            if (i == 0) {
                rspMsg.setError();
                rspMsg.setMsg("密码修改失败");
                return rspMsg;
            }

            rspMsg.setOK();
            rspMsg.setMsg("密码修改成功");
            return rspMsg;
        }

    }


    // 查询所有用户
    @Override
    public RspMsg<UserInfo> selectUsersfind(){
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<UserInfo> userList =userInfoMapper.selectAll();
        if (userList.isEmpty() || userList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关数据");
            return rspMsg;
        }

        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(userList);
        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("userList", userList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }








    // 根据多条件查找用户
    @Override
    public RspMsg<List<UserInfo>> findUser(String id, String email) {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<UserInfo> userList = userInfoMapper.findUser(id, email);
        if (userList.isEmpty() || userList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关用户");
            return rspMsg;
        }

        PageInfo<UserInfo> pageInfo = new PageInfo<UserInfo>(userList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("userList", userList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }

    // 修改密码（忘记密码）
    @Override
    public RspMsg<UserInfo> forgetPassword(String userId) {
        RspMsg rspMsg = new RspMsg();
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setPassword("111111");
        userInfo.setRemark1(UUID.randomUUID().toString().replaceAll("-", ""));
        userInfo.setPassword(Md5Utils.encryptPassword(userInfo.getPassword(), userInfo.getRemark1()));
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (i > 0) {
            rspMsg.setOK();
            return rspMsg;
        }

        rspMsg.setError();
        rspMsg.setMsg("密码重置失败");
        return rspMsg;
    }


    // 新增支付密码

    @Override
    public    RspMsg<UserInfo> addpayPasswordInfo(String userinfoId,String payPassword){
        RspMsg rspMsg = new RspMsg();
        String email="";
        UserInfo userInfo = userInfoMapper.findUser(userinfoId,email).get(0);
            userInfo.setPayPassword(payPassword);
            // 将新密码生成盐值并且加密
            userInfo.setRemark2(UUID.randomUUID().toString().replaceAll("-", ""));
            userInfo.setPayPassword(Md5Utils.encryptPassword(userInfo.getPayPassword(), userInfo.getRemark2()));
            int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
            if (i == 0) {
                rspMsg.setError();
                rspMsg.setMsg("支付密码新增失败");
                return rspMsg;
            }
            rspMsg.setOK();
            rspMsg.setMsg("支付密码新增成功");
            return rspMsg;
    }
}
